package cn.stronglink.asset.module.report.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;

import cn.stronglink.asset.model.ReportFile;
import cn.stronglink.asset.module.report.service.ReportService;
import cn.stronglink.asset.module.report.vo.ReportFileListVo;
import cn.stronglink.asset.module.report.vo.ReportTempletVo;
import cn.stronglink.suite.core.base.AbstractController;

@Controller
@RequestMapping(value = "report")
public class ReportController extends AbstractController {
	
	@Autowired
	private ReportService reportService;
	
	/**
	 * 查询所有报告模板并分页
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "queryList")
	public ResponseEntity<ModelMap> queryList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Map<String, Object> params) {	
		@SuppressWarnings("unchecked")
		Page<ReportTempletVo> page = (Page<ReportTempletVo>) super.getPage(params);
		Page<ReportTempletVo> data = reportService.queryList(page,params);
		return setSuccessModelMap(modelMap, data);
	}
	
	/**
	 * 根据报告模板Id查询报告模板
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "qryById")
	public ResponseEntity<ModelMap> qryById(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Long id) {	
		ReportTempletVo data =reportService.qryById(id);
		return setSuccessModelMap(modelMap, data);
	}

	/**
	 * 创建报告模板
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param reportTempletVo
	 * @return
	 */
	@RequestMapping(value = "create")
	public ResponseEntity<ModelMap> create(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody ReportTempletVo reportTempletVo) {
		reportService.create(reportTempletVo);
		return setSuccessModelMap(modelMap, null);
	}
	

	/**
	 * 修改报告模板
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param reportTempletVo
	 * @return
	 */
	@RequestMapping(value = "edit")
	public ResponseEntity<ModelMap> edit(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody ReportTempletVo reportTempletVo) {
		reportService.edit(reportTempletVo);
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 生成报告文件
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param reportTempletId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "buildReportFile")
	public ResponseEntity<ModelMap> buildReportFile(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Long id) throws Exception {
		reportService.buildReportFile(id);
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 删除报告模板
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "remove")
	public ResponseEntity<ModelMap> remove(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Long id) {
		reportService.remove(id);
		return setSuccessModelMap(modelMap, null);
	}

	/**
	 * 查询报告文件
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "queryReportFileList")
	public ResponseEntity<ModelMap> queryReportFileList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Map<String, Object> params) {	
		@SuppressWarnings("unchecked")
		Page<ReportFileListVo> page = (Page<ReportFileListVo>) super.getPage(params);
		Page<ReportFileListVo> data = reportService.queryReportFileList(page,params);
		return setSuccessModelMap(modelMap, data);
	}
	
	/**
	 * 删除报告文件
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param reportFile
	 * @return
	 */
	@RequestMapping(value = "removeFile")
	public ResponseEntity<ModelMap> removeFile(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody ReportFile reportFile) {
		reportService.removeFile(reportFile);
		File file = new File(reportFile.getFilePath());  
		if(file.exists()) {
			file.delete();
		}
		return setSuccessModelMap(modelMap, null);
	}

	/**
	 * 报告文件下载
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param filePath
	 * @return
	 */
	@RequestMapping(value = "downLoadFile")
	public void downLoadFile(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			String param = request.getParameter("param").toString();
			//String param = new String(p.getBytes("ISO8859-1"), "UTF-8");
			Map<String, Object> params =  JSON.parseObject(param, Map.class);
			String filePath = params.get("filePath").toString();
            // path是指欲下载的文件的路径。
            File file = new File(filePath);
            // 取得文件名。
            String filename = file.getName();
            // 取得文件的后缀名。
            String ext = filename.substring(filename.lastIndexOf(".") + 1);
            String downLoadName = params.get("fileName").toString();
            filename = downLoadName+"."+ext;
            // 以流的形式下载文件。
            InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            // response.reset();
            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("GB2312"),"ISO8859-1"));
            response.addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
	}
	
}
